import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kasax/commons/commons.dart';
import 'package:dio/dio.dart';
import 'package:kasax_demos2/generated/json/wmdb_film_result_entity_helper.dart';
import 'package:kasax_demos2/pratacties/douban/entrys/wmdb_film_result_entity.dart';
import 'package:kasax_demos2/pratacties/douban/pages/details/page_film_details.dart';

class PageHome extends StatefulWidget {
  const PageHome({Key? key}) : super(key: key);

  @override
  _PageHomeState createState() => _PageHomeState();
}

class _PageHomeState extends State<PageHome> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      builder: (BuildContext context,
          AsyncSnapshot<List<WmdbFilmResultEntity>> snapshot) {
        if (snapshot.hasData) {
          var list = snapshot.data;
          if (list != null) {
            return ListView.separated(
                itemBuilder: (context, index) {
                  var item = list[index];
                  var filmInfo = item.data[0];
                  var filmAlisa = "";
                  if (null != item.alias) {
                    filmAlisa = "${filmInfo.genre} >> ${item.alias}";
                  } else {
                    filmAlisa = "${filmInfo.genre}";
                  }
                  double rating = double.parse(item.doubanRating) * 10 / 100;
                  print("rating: $rating");
                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) => PageMovieDetails(item)));
                    },
                    child: Container(
                      padding: EdgeInsets.only(left: 20, right: 20, top: 10),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          SizedBox(
                            width: 80,
                            height: 120,
                            child: Image.network(
                              filmInfo.poster,
                              fit: BoxFit.cover,
                            ),
                          ),
                          Expanded(
                            child: Container(
                              margin: EdgeInsets.only(left: 5, right: 5),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    children: [
                                      Text(
                                        filmInfo.name,
                                        style: TextStyle(color: Colors.black),
                                      ),
                                      Text(
                                        "(${item.year})",
                                        style: TextStyle(color: Colors.grey),
                                      ),
                                    ],
                                  ),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Row(
                                    children: [
                                      SizedBox(
                                        width: 35,
                                        height: 8,
                                        child: ClipRRect(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(12)),
                                            child: LinearProgressIndicator(
                                              value: rating,
                                              valueColor:
                                                  AlwaysStoppedAnimation<Color>(
                                                      Colors.orange),
                                              backgroundColor: Colors.grey,
                                            )),
                                      ),
                                      Text("${item.doubanRating}"),
                                    ],
                                  ),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Text(
                                    "$filmAlisa",
                                    overflow: TextOverflow.ellipsis,
                                    maxLines: 3,
                                    style: TextStyle(fontSize: 12),
                                  )
                                ],
                              ),
                            ),
                          ),
                          Container(
                            child: Column(
                              children: [
                                Icon(
                                  Icons.star,
                                  color: Colors.orangeAccent,
                                ),
                                Text(
                                  "想看",
                                  style: TextStyle(color: Colors.orangeAccent),
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                  );
                },
                separatorBuilder: (context, index) {
                  return Divider();
                },
                itemCount: list.length);
          } else {
            return buildLoadingWidget();
          }
        }
        return buildLoadingWidget();
      },
      future: featchMovieList(),
    );
  }

  Future<List<WmdbFilmResultEntity>> featchMovieList() async {
    var dio = Dio();
    var response = await dio.get(
        "https://api.wmdb.tv/api/v1/top?type=Imdb&skip=0&limit=50&lang=Cn");
    print("response status code : ${response.statusCode}");

    List<WmdbFilmResultEntity> list = [];
    if (response.statusCode == 200) {
      try {
        List datas = response.data;
        for (var value in datas) {
          WmdbFilmResultEntity entity = WmdbFilmResultEntity();
          list.add(wmdbFilmResultEntityFromJson(entity, value));
        }
      } catch (e) {
        print(e);
      }
    }
    return list;
  }
}
